<?php
class FieldService
{
	private $modelId;

	private $fieldName;

	private $fieldType;

	private $displayName;

	/**
	 * 根据模版的id查找出全部属于该模版字段	
	 * @Author   Leikes
	 * @DateTime 2016-01-13T15:43:21+0800
	 * @return   object                   返回一个对象的结果集
	 */
	public function findAll()
	{
		$fields=Field::where('model_id','=',$this->modelId)->get();
		return $fields;
	}

	public function findByOne($fieldId)
	{
		$validator = Validator::make(
		    array('fieldId' => $fieldId ),
		    array('fieldId' => 'required|exists:field,id' )
		);

		if($validator->fails())
		{
			return  CMSException::$findFieldErr;
		}
		$field=Field::find($fieldId);
		return $field;
	}

	/**
	 * 增加字段
	 * @Author   Leikes
	 * @DateTime 2016-01-13T17:04:05+0800
	 */
	public function addField()
	{
		try
		{
			DB::transaction(function()
			{
				$isSuccess=$this->createField();
				if($isSuccess==false)
				{
					throw Exception;
				}
				$field=new Field;
				$field->model_id=$this->modelId;
				$field->field_name=$this->fieldName;
				$field->field_type=$this->fieldType;
				$field->display_name=$this->displayName;
				$field->save();

			});
		}
		catch(Exception $e)
		{
			return CMSException::$addFieldErr;
		}

		return CMSException::$ok;
		
	}

	private function createField()
	{
		try
		{
			$model=Model::find($this->modelId);
			Schema::table($model->table_name,function($table)
			{
				$temp=$this->fieldType;
				$table->$temp($this->fieldName);
			});
		}
		catch(Exception $e)
		{
			return false;
		}
		return true;
	}

	/**
	 * 删除字段
	 * @Author   Leikes
	 * @DateTime 2016-01-14T09:12:55+0800
	 * @param    integer                   $fieldId 字段的id
	 * @return   array                            errCode
	 */
	public function deleteField($fieldId)
	{
		try
		{
			DB::transaction(function() use($fieldId) 
			{
				$field =Field::find($fieldId);
				$model =Model::find($field->model_id);
				Schema::table($model->table_name,function($table) use($field)
				{
					$table->dropColumn($field->field_name);
				});

				$field->delete();
			});

		}
		catch(Exception $e)
		{
			return CMSException::$deleteFieldErr;
		}

		return CMSException::$ok;
	}

	/**
	 * 修改模板字段类型和字段名字(注：修改表字段类型会把原来字段删除再新建个字段)
	 * @Author   Leikes
	 * @DateTime 2016-01-14T10:47:45+0800
	 * @param    Integer                   $fieldId 字段id
	 * @return   errCode                            错误码
	 */
	public function updateField($fieldId)
	{
		try
		{
			DB::transaction(function() use($fieldId)
			{
			    $field=Field::find($fieldId);

				$model=Model::find($field->model_id);
				if($field->field_type!==$this->fieldType)
				{
					Schema::table($model->table_name, function($table) use($field)
					{
					    $table->dropColumn($field->field_name);
					});

					if($field->field_name!==$this->fieldName)
					{
						Schema::table($model->table_name, function($table)
						{
							$temp=$this->fieldType;
						    $table->$temp($this->fieldName);
						});
					}
					else
					{
						Schema::table($model->table_name, function($table) use($field)
						{
							$temp=$this->fieldType;
						    $table->$temp($field->field_name);
						});
					}
				}
				else
				{
					if($field->field_name!==$this->fieldName)
					{
						Schema::table($model->table_name, function($table) use($field)
						{
						    $table->renameColumn($field->field_name, $this->fieldName);
						});
					}
				}
				
				$field->field_name   =$this->fieldName;
				$field->field_type   =$this->fieldType;
				$field->display_name =$this->displayName;
				$field->save();
			});
			
		}
		catch(Exception $e)
		{
			return CMSException::$updateFieldErr;
		}

		return CMSException::$ok;

	}

	public function setModelId($modelId)
	{
		$this->modelId=$modelId;
	}

	public function setFieldName($fieldName)
	{
		$this->fieldName=$fieldName;
	}

	public function setFieldType($fieldType)
	{
		$this->fieldType=$fieldType;
	}

	public function setDisplayName($displayName)
	{
		$this->displayName=$displayName;
	}
}
?>